package uniandes.dermatologia;

import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
	
	private EditText txtUserName;
	
	private EditText txtPassword;
	
	private Button btnLogin;
	
	private AlertDialog dialog;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		txtUserName = (EditText)findViewById(R.id.txtUsername);
		
		txtPassword = (EditText)findViewById(R.id.txtPassword);
		
		btnLogin = (Button)findViewById(R.id.btnLogin);
		
		btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				login(v);
			}
		});
		
		getActionBar().setDisplayHomeAsUpEnabled(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	public void login(View v) {
		String userName = txtUserName.getText().toString();
		String password = txtPassword.getText().toString();
		if(userName == null || userName.equals("") || password == null || password.equals(""))
		{
			createDialog("Error", "Debe llenar todos los campos.");
		}
		else
		{
			final DermatologiaUniandesApp app = (DermatologiaUniandesApp)getApplication();
			app.login(userName, password, this);			
		}
	}
	
	private void createDialog(String title, String message )
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setCancelable(true);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }
	
}
